package io.yorkecao.sample.web;

import io.yorkecao.sample.service.ShiroSampleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Yorke
 */
@RestController
public class ShiroSampleController {

    @Autowired
    private ShiroSampleService shiroSampleService;

    @PostMapping("login")
    public void login(String username, String password, boolean rememberMe) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        token.setRememberMe(rememberMe);
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);
    }

    @GetMapping("logout")
    public void logout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
    }

    @GetMapping("read")
    public String read() {
        return this.shiroSampleService.read();
    }

    @GetMapping("write")
    public String write() {
        return this.shiroSampleService.write();
    }

    @GetMapping("remove")
    public String remove() {
        return this.shiroSampleService.remove();
    }

    @GetMapping("patch")
    public String patch() {
        return this.shiroSampleService.patch();
    }

    @GetMapping("remove/patch")
    public String superWorker() {
        return this.shiroSampleService.superWorker();
    }

    @GetMapping("read/write")
    public String host() {
        return this.shiroSampleService.host();
    }

    @GetMapping("everything")
    public String god() {
        return this.shiroSampleService.god();
    }

}
